package com.yueze.demo1.util;

/*import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;*/

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author Administrator
 * @create 2020-07-27
 * 15:37
 * @desc
 **/
public class WaterMark {

    /*
     * 给图片添加水印
     * @param filePath 需要添加水印的图片的路径
     * @param markContent 水印的文字
     * @param markContentColor 水印文字的颜色
     * @param qualNum 图片质量
     * @return
     */
    public boolean createMark(String filePath,String markContent,Color markContentColor,float qualNum,String watermark)
    {
        ImageIcon imgIcon=new ImageIcon(filePath);
        Image theImg =imgIcon.getImage();
        File picture = new File(filePath);
        BufferedImage sourceImg = null;
        try {
            sourceImg = ImageIO.read(new FileInputStream(picture));


        } catch (IOException e) {
            e.printStackTrace();
        }
        int width=sourceImg.getWidth();
        int height= sourceImg.getHeight();
        //ImageIcon waterIcon=new ImageIcon(watermark);
        //Image waterImg =waterIcon.getImage();
        BufferedImage bimage = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g=bimage.createGraphics();
        //设置字体大小
        g.setFont(new Font("宋体", Font.BOLD, 36));
        g.setColor(markContentColor);
        g.setBackground(Color.red);
        g.drawImage(theImg, 0, 0,width,height,null);
        //g.drawImage(waterImg, width*2, height, null );
        System.out.println(width+"========================"+height);
        g.drawString(markContent,width-300,height-10); //添加水印的文字和设置水印文字出现的内容
        g.dispose();
        try{
            FileOutputStream os = new FileOutputStream(filePath);
            // 生成图片
            ImageIO.write(bimage, "JPEG", os);
            os.close();
/*            FileOutputStream out=new FileOutputStream(filePath);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
            param.setQuality(qualNum, true);
            encoder.encode(bimage, param);
            out.close();*/
        }
        catch(Exception e)
        {
            return false;
        }
        return true;
    }

}
